/**
 * Created by Administrator on 2017/10/11.
 */
/**
 * Created by Administrator on 2017/10/10.
 */
'use strict';
let fs = require('fs');
let Response = require('../../../model/Response');
let RestifyProxy = require('../../../util/RestifyProxy');
let CustomPageBean = require('../../../model/CustomPageBean');
let RentalService = require('../../../service/RentalService');
let logger = require('../../../util/LoggerUtil').logger('RentalController.js');
const URI = {
    RENTAL_CONTRACT_UPLOAD:'/contract/files/upload', /* 查询托盘租赁详情合同列表 */
};
/**
/**
 * 托盘租赁页面
 */
module.exports.rentalPage = function () {
    let response = new Response();
    response.render('back/rent/rental/rental');
    this.resolve(response);
};
/**
 * 租赁列表
 */
module.exports.list  = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await RentalService.getRentalList(this, params);
        let pageBean = new CustomPageBean(result);
        response.send(pageBean);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};
/**
 * 租赁订单-详情页面
 */
module.exports.detailRentalPage = function () {
    let response = new Response();
    response.render('back/rent/rental/detail-rental');
    this.resolve(response);
};

/* 通过订单ID，获取订单详情*/

module.exports.getRentalData = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await RentalService.getRentalDataById(this, params.orderNo);
        response.send(result);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};
/* 通过订单ID，获取订单详情托盘租金数据*/

module.exports.getPalletMoney = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await RentalService.getPalletById(this, params.totalQuantity);
        response.send(result);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};
/**
 * 创建新增租赁订单-页面
 */
module.exports.clientCreateRentalPage = function () {
    let response = new Response();
    response.render('back/rent/rental/client-create-rental');
    this.resolve(response);
};
/**
 * 下一步租赁订单-页面
 */
module.exports.clientNextRentalPage = function () {
    let response = new Response();
    response.render('back/rent/rental/client-next-rental');
    this.resolve(response);
};
/**
 * 创建新增订单-下一步保存方法
 */
module.exports.clientNextRentalSave = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await RentalService.createRental(this, params);
        response.send(result);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};
/**
 * 租赁订单详情订单变更记录-页面
 */
module.exports.changeOrderPage = function () {
    let response = new Response();
    response.render('back/rent/rental/change-order');
    this.resolve(response);
};
//恢复订单
module.exports.recoverOrder = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await RentalService.getRecoverOrderById(this, params.orderLeaseId);
        response.send(result);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};
//废弃订单
module.exports.cancleOrder = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await RentalService.getCancleOrderById(this, params.orderLeaseId);
        response.send(result);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};

//确认收板
module.exports.sureOrder = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await RentalService.getsureOrderById(this, params.orderNo);
        response.send(result);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};
//订单修改
module.exports.editSave = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await RentalService.editSave(this, params);
        response.send(result);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};
//订单金额计算
module.exports.getCost = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await RentalService.getCost(this, params);
        response.send(result);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};
/* 通过订单ID，获取续订订单详情*/

module.exports.getContinuedData = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await RentalService.getContinuedDataById(this, params.orderNo);
        response.send(result);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};
/**
 * 租赁订单详情续租-列表
 */
module.exports.Rentallist  = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await RentalService.getRenewalOrderList(this, params);
        let pageBean = new CustomPageBean(result);
        response.send(pageBean);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};
/**
 * 租赁订单详情退板-列表
 */
module.exports.Returnlist  = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await RentalService.getOrderReturnList(this, params);
        let pageBean = new CustomPageBean(result);
        response.send(pageBean);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};
/**
 * 租赁订单详情退板-列表
 */
module.exports.Subletlist  = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await RentalService.getOrderSubletList(this, params);
        let pageBean = new CustomPageBean(result);
        response.send(pageBean);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};
/**
 * 租赁订单详情合同扫描件-页面
 */
module.exports.contractManagementPage = function () {
    let response = new Response();
    response.render('back/rent/rental/contract-management');
    this.resolve(response);
};

/**
 * 租赁订单详情合同扫描件列表
 */
module.exports.contractManagementList  = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await RentalService.getContractManagementList(this, params);
        let pageBean = new CustomPageBean(result);
        response.send(pageBean);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};
/**
 * 租赁订单详情合同扫描件-上传
 */
module.exports.contractManagementUpload  = async function () {
    let req = this.req;
    let files = req.files;
    let restify = new RestifyProxy(this);
    let params = req.body;
    for (let key in files) {
        params[key] = {
            value: fs.createReadStream(files[key].path),
            options: {
                filename: files[key].name,
                contentType: files[key].type
            }
        };
    }
    let response = new Response();
    restify.setUrl(URI.RENTAL_CONTRACT_UPLOAD)
        .setFormData(params)
        .uploadFile(function (result) {
            response.send(result);
            this.resolve(response);
        }.bind(this));
};
module.exports.contractManagementDelete = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await RentalService.deleteContractManagement(this, params.id);
        response.send(result);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};

/**
 * 租赁订单详情合同扫描件-页面-预览
 */
module.exports.contractManagementPreviewPage = function () {
    let response = new Response();
    response.render('back/rent/rental/contract-picture');
    this.resolve(response);
};
//订单导出
module.exports.exportRentalData = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await RentalService.getExportDataById(this, params.orderNo);
        response.send(result);
       /* docx.generate (response)*/
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};
//绑定订单页面
module.exports.bindRentalPage= function () {
    let response = new Response();
    response.render('back/rent/rental/bind-order');
    this.resolve(response);
};
//绑定订单页面列表
module.exports.bindRentalList  = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await RentalService.getBindRental(this, params);
        let pageBean = new CustomPageBean(result);
        response.send(pageBean);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};
//可绑定订单页面列表
module.exports.bindRentalCanlist  = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await RentalService.getCanBindList(this, params);
        let pageBean = new CustomPageBean(result);
        response.send(pageBean);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};

module.exports.SubmitList = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await RentalService.SubmitList(this, params);
        response.send(result);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};

//解除绑定订单页面
module.exports.offBindRentalPage= function () {
    let response = new Response();
    response.render('back/rent/rental/off-bind-order');
    this.resolve(response);
};
//解除绑定订单页面列表
module.exports.offBindRental  = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await RentalService.getOffBindRental(this, params);
        response.send(result);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};
